<?php

namespace app\controller\admin\lol;

use app\model\api\lol\LolHero;
use app\model\api\lol\LolHeroRelation;
use app\model\api\lol\LolHeroSkin;
use app\model\api\lol\LolHeroTeam;
use laytp\controller\Backend;
use laytp\library\CommonFun;
use think\facade\Db;

/**
 * Class Career
 * @package app\controller\admin\ah
 */
class Hero extends Backend
{
    protected $model;
    public function initialize()
    {
        $this->model = new LolHero();
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    public function add()
    {
        $post = CommonFun::filterPostData($this->request->post());

        $insData = [
            'name' => $post['name'],
            'hero_id' => $post['hero_id'],
            'nickname' => $post['nickname'],
            'type' => $post['type'],
            'difficulty' => $post['difficulty'],
            'url' => getRelativeUrl($post['url']),
            'icon' => getRelativeUrl($post['icon']),
            'create_time' => date("Y-m-d H:i:s"),
        ];
        try {

            Db::startTrans();
            $id = $this->model->insertGetId($insData);
            $team_list = [];
            $restraint_list = [];
            if(!is_array($post['team']))$post['team'] = explode(",",$post['team']);
            if(count($post['team'])>0){
                foreach ($post['team'] as $k=>$item){
                    $team_list[] =[
                      'hero_id'=>$id,
                      'relation_hero_id'=>$item,
                      'sort'=>$k,
                    ];
                }
            }
            if(!is_array($post['restraint']))$post['restraint'] = explode(",",$post['restraint']);
            if(count($post['restraint'])>0){
                foreach ($post['restraint'] as $k=>$item){
                    $restraint_list[] =[
                        'hero_id'=>$id,
                        'relation_hero_id'=>$item,
                        'sort'=>$k,
                        'type'=>1,
                    ];
                }
            }
            if(!is_array($post['restrained']))$post['restrained'] = explode(",",$post['restrained']);
            if(count($post['restrained'])>0){
                foreach ($post['restrained'] as $k=>$item){
                    $restraint_list[] =[
                        'hero_id'=>$id,
                        'relation_hero_id'=>$item,
                        'sort'=>$k,
                        'type'=>2,
                    ];
                }
            }
            if(count($team_list)>0)(new LolHeroTeam())->insertAll($team_list);
            if(count($restraint_list)>0)(new LolHeroRelation())->insertAll($restraint_list);
            if (!$id) throw new \Exception('保存失败');
//            $post['skin_url1'] = parse_url($post['skin_url1']);
//            $post['skin_url2'] = parse_url($post['skin_url2']);
//            $post['skin_url3'] = parse_url($post['skin_url3']);
//            $post['skin_url4'] = parse_url($post['skin_url4']);
//            $post['skin_url5'] = parse_url($post['skin_url5']);
//            $post['skin_url6'] = parse_url($post['skin_url6']);
//            $post['skin_url7'] = parse_url($post['skin_url7']);
//            $post['skin_url8'] = parse_url($post['skin_url8']);
//            $post['skin_url9'] = parse_url($post['skin_url9']);
//            $post['skin_url10'] = parse_url($post['skin_url10']);
            $skinList = [];
            $sort = 0;
            if (!empty($post['skin_name1'])) {
                array_push($skinList, ['name' => $post['skin_name1'],'hero_id'=>$id,'sort'=>++$sort,'url' => $post['skin_url1']]);
            }
            if (!empty($post['skin_name2'])) {
                array_push($skinList, ['name' => $post['skin_name2'],'hero_id'=>$id,'sort'=>++$sort,'url' => $post['skin_url2']]);
            }
            if (!empty($post['skin_name3'])) {
                array_push($skinList, ['name' => $post['skin_name3'],'hero_id'=>$id,'sort'=>++$sort,'url' => $post['skin_url3']]);
            }
            if (!empty($post['skin_name4'])) {
                array_push($skinList, ['name' => $post['skin_name4'],'hero_id'=>$id,'sort'=>++$sort,'url' => $post['skin_url4']]);
            }
            if (!empty($post['skin_name5'])) {
                array_push($skinList, ['name' => $post['skin_name5'],'hero_id'=>$id,'sort'=>++$sort,'url' => $post['skin_url5']]);
            }
            if (!empty($post['skin_name6'])) {
                array_push($skinList, ['name' => $post['skin_name6'],'hero_id'=>$id,'sort'=>++$sort,'url' => $post['skin_url6']]);
            }
            if (!empty($post['skin_name7'])) {
                array_push($skinList, ['name' => $post['skin_name7'],'hero_id'=>$id,'sort'=>++$sort,'url' => $post['skin_url7']]);
            }
            if (!empty($post['skin_name8'])) {
                array_push($skinList, ['name' => $post['skin_name8'],'hero_id'=>$id,'sort'=>++$sort,'url' => $post['skin_url8']]);
            }
            if (!empty($post['skin_name9'])) {
                array_push($skinList, ['name' => $post['skin_name9'],'hero_id'=>$id,'sort'=>++$sort,'url' => $post['skin_url9']]);
            }
            if (!empty($post['skin_name10'])) {
                array_push($skinList, ['name' => $post['skin_name10'],'hero_id'=>$id,'sort'=>++$sort,'url' => $post['skin_url10']]);
            }
            foreach ($skinList as &$item){
                $item['create_time'] = date("Y-m-d H:i:s");
            }
            if(count($skinList)>0)(new LolHeroSkin())->insertAll($skinList);
            $this->clearCache();
            Db::commit();
            return $this->success('保存成功');
        } catch (\Exception $e) {
            Db::rollback();
            return $this->error($e->getMessage());
        }
    }



    //设置展示
    public function setIsShow()
    {
        $id       = $this->request->post('id');
        $fieldVal = $this->request->post('field_val');
        $isRecycle = $this->request->post('is_recycle');
        $update['is_show'] = $fieldVal;

        try {
            if($isRecycle) {
                $updateRes = $this->model->onlyTrashed()->where('id', '=', $id)->update($update);
            } else {
                $updateRes = $this->model->where('id', '=', $id)->update($update);
            }

            if ($updateRes) {
                $this->clearCache();
                return $this->success('操作成功');
            } else if ($updateRes === 0) {
                return $this->success('未作修改');
            } else {
                return $this->error('操作失败');
            }
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }

    public function edit(){
        $post     = CommonFun::filterPostData($this->request->post());
        $detail = $this->model->find($post['id']);
        empty($detail) && $this->error('数据不存在');
        $detail->name = $post['name'];
        $detail->nickname = $post['nickname'];
        $detail->hero_id = $post['hero_id'];
        $detail->type = $post['type'];
        $detail->difficulty = $post['difficulty'];
        $detail->icon = getRelativeUrl($post['icon']);
        $detail->url = getRelativeUrl($post['url']);
        try {
            $team_model = new LolHeroTeam();
            $relation_model = new LolHeroRelation();
            Db::startTrans();
            $save = $detail->save();
            if (!$save) throw new \Exception('保存失败');
            $team_model->destroy($team_model->where('hero_id',$post['id'])->column("id"));
            $relation_model->destroy($relation_model->where('hero_id',$post['id'])->column("id"));
            $team_list = [];
            $restraint_list = [];
            if(!is_array($post['team']))$post['team'] = explode(",",$post['team']);
            if(count($post['team'])>0){
                foreach ($post['team'] as $k=>$item){
                    $team_list[] =[
                        'hero_id'=>$post['id'],
                        'relation_hero_id'=>$item,
                        'sort'=>$k,
                    ];
                }
            }
            if(!is_array($post['restraint']))$post['restraint'] = explode(",",$post['restraint']);
            if(count($post['restraint'])>0){
                foreach ($post['restraint'] as $k=>$item){
                    $restraint_list[] =[
                        'hero_id'=>$post['id'],
                        'relation_hero_id'=>$item,
                        'sort'=>$k,
                        'type'=>1,
                    ];
                }
            }
            if(!is_array($post['restrained']))$post['restrained'] = explode(",",$post['restrained']);
            if(count($post['restrained'])>0){
                foreach ($post['restrained'] as $k=>$item){
                    $restraint_list[] =[
                        'hero_id'=>$post['id'],
                        'relation_hero_id'=>$item,
                        'sort'=>$k,
                        'type'=>2,
                    ];
                }
            }
            if(count($team_list)>0)$team_model->insertAll($team_list);
            if(count($restraint_list)>0)$relation_model->insertAll($restraint_list);
//            $post['skin_url1'] = parse_url($post['skin_url1']);
//            $post['skin_url2'] = parse_url($post['skin_url2']);
//            $post['skin_url3'] = parse_url($post['skin_url3']);
//            $post['skin_url4'] = parse_url($post['skin_url4']);
//            $post['skin_url5'] = parse_url($post['skin_url5']);
//            $post['skin_url6'] = parse_url($post['skin_url6']);
//            $post['skin_url7'] = parse_url($post['skin_url7']);
//            $post['skin_url8'] = parse_url($post['skin_url8']);
//            $post['skin_url9'] = parse_url($post['skin_url9']);
//            $post['skin_url10'] = parse_url($post['skin_url10']);
            $skinList = [];
            $sort = 0;
            if (!empty($post['skin_name1'])) {
                array_push($skinList, ['name' => $post['skin_name1'],'hero_id'=>$post['id'],'sort'=>++$sort,'url' => $post['skin_url1']]);
            }
            if (!empty($post['skin_name2'])) {
                array_push($skinList, ['name' => $post['skin_name2'],'hero_id'=>$post['id'],'sort'=>++$sort,'url' => $post['skin_url2']]);
            }
            if (!empty($post['skin_name3'])) {
                array_push($skinList, ['name' => $post['skin_name3'],'hero_id'=>$post['id'],'sort'=>++$sort,'url' => $post['skin_url3']]);
            }
            if (!empty($post['skin_name4'])) {
                array_push($skinList, ['name' => $post['skin_name4'],'hero_id'=>$post['id'],'sort'=>++$sort,'url' => $post['skin_url4']]);
            }
            if (!empty($post['skin_name5'])) {
                array_push($skinList, ['name' => $post['skin_name5'],'hero_id'=>$post['id'],'sort'=>++$sort,'url' => $post['skin_url5']]);
            }
            if (!empty($post['skin_name6'])) {
                array_push($skinList, ['name' => $post['skin_name6'],'hero_id'=>$post['id'],'sort'=>++$sort,'url' => $post['skin_url6']]);
            }
            if (!empty($post['skin_name7'])) {
                array_push($skinList, ['name' => $post['skin_name7'],'hero_id'=>$post['id'],'sort'=>++$sort,'url' => $post['skin_url7']]);
            }
            if (!empty($post['skin_name8'])) {
                array_push($skinList, ['name' => $post['skin_name8'],'hero_id'=>$post['id'],'sort'=>++$sort,'url' => $post['skin_url8']]);
            }
            if (!empty($post['skin_name9'])) {
                array_push($skinList, ['name' => $post['skin_name9'],'hero_id'=>$post['id'],'sort'=>++$sort,'url' => $post['skin_url9']]);
            }
            if (!empty($post['skin_name10'])) {
                array_push($skinList, ['name' => $post['skin_name10'],'hero_id'=>$post['id'],'sort'=>++$sort,'url' => $post['skin_url10']]);
            }
            foreach ($skinList as &$item){
                $item['create_time'] = date("Y-m-d H:i:s");
            }
            //(new LolHeroSkin())->where('hero_id',$post['id'])->delete();
            if(count($skinList)>0)(new LolHeroSkin())->insertAll($skinList);
            $this->clearCache();
            Db::commit();
            return $this->success('保存成功');
        } catch (\Exception $e) {
            Db::rollback();
            return $this->error($e->getMessage());
        }
    }

    //查看详情
    public function info()
    {
        $id   = $this->request->param('id');
        $info = $this->model->with(['relation','team'])->find($id);
        if(is_object($info))$info= $info->toArray();
        if(isset($info['icon'])){
            $info['icon'] = imgUrlToAbsolute($info['icon']);
        }
        if(isset($info['url'])){
            $info['url'] = imgUrlToAbsolute($info['url']);
        }
        $info['team'] = array_column($info['team'],"relation_hero_id");
        $info['restraint'] = [];
        $info['restrained'] = [];
        foreach ($info['relation'] as $item){
            IF($item['type']==1){
                $info['restraint'][] = $item['relation_hero_id'];
            }else{
                $info['restrained'][] = $item['relation_hero_id'];
            }
        }
        $info['skinsList'] = (new LolHeroSkin())->where('hero_id',$id)->order("sort")->select()->toArray();
        for ($i = 1; $i <= 10; $i++) {
            $info['skin_name' . $i] = !empty($info['skinsList'][$i - 1]['name']) ? $info['skinsList'][$i - 1]['name'] : '';
            $info['skin_url' . $i] = !empty($info['skinsList'][$i - 1]['url']) ? imgUrlToAbsolute($info['skinsList'][$i - 1]['url']) : '';
        }
        return $this->success('获取成功', $info);
    }

    public function difficulty(){
        return $this->success('获取难度',[
            ['difficulty'=>'简单'],
            ['difficulty'=>'中等'],
            ['difficulty'=>'困难'],
        ]);
    }

    public function type(){
        return $this->success('获取职责',[
            ['type'=>'刺客'],
            ['type'=>'坦克'],
            ['type'=>'射手'],
            ['type'=>'战士'],
            ['type'=>'法师'],
            ['type'=>'辅助'],
        ]);
    }

    public function relation(){
        return $this->success('获取关系类型',[
            ['key'=>1,'克制'],
            ['key'=>2,'被克制'],
        ]);
    }


    //设置排序
    public function setSort()
    {
        $id       = $this->request->post('id');
        $fieldVal = $this->request->post('field_val');
        $isRecycle = $this->request->post('is_recycle');
        $update['list_sort'] = $fieldVal;

        try {
            if($isRecycle) {
                $updateRes = $this->model->onlyTrashed()->where('id', '=', $id)->update($update);
            } else {
                $updateRes = $this->model->where('id', '=', $id)->update($update);
            }

            if ($updateRes) {
                $this->clearCache();
                return $this->success('操作成功');
            } else if ($updateRes === 0) {
                return $this->success('未作修改');
            } else {
                return $this->error('操作失败');
            }
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }
}